package com.fc.mall.decoration.page.rs;

import com.fc.mall.base.object.Result;
import com.fc.mall.decoration.page.model.CustomPage;
import com.fc.mall.decoration.page.object.CustomPageCreate;
import com.fc.mall.decoration.page.object.CustomPageInfo;
import com.fc.mall.decoration.page.object.CustomPageUpdate;
import com.fc.mall.decoration.page.service.CustomPageService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import static com.fc.mall.util.Constants.FALSE;
import static com.fc.mall.util.Constants.TRUE;

@Validated
@RestController
@RequestMapping("/admin/pages")
public class AdminCustomPageResource {
    @Autowired
    CustomPageService customPageService;

    @Autowired
    MapperFacade mapperFacade;

    @ApiOperation("创建页面")
    @PostMapping
    public Result createPage(@Valid @RequestBody CustomPageCreate customPageCreate) {
        CustomPage customPage = customPageService.createCustomPage(customPageCreate);
        CustomPageInfo customPageInfo = mapperFacade.map(customPage, CustomPageInfo.class);

        if(customPage.getIsDefault()){ //如果是默认页则把原来默认页设成非默认
            customPageService.removeDefaultPages(customPage.getId());
        }

        return Result.success(customPageInfo);
    }

    @ApiOperation("根据id来获取页面")
    @GetMapping("/{pageId}")
    public Result getCustomPageInfo(
            @ApiParam(name = "pageId", required = true, value = "PageId") @PathVariable("pageId") String pageId,
            @ApiParam(name = "activeOnly", defaultValue = "true") @RequestParam(name = "activeOnly", defaultValue = TRUE) boolean activeOnly) {
        CustomPage customPage = customPageService.getCustomPage(pageId, activeOnly,null);
        CustomPageInfo customPageInfo = mapperFacade.map(customPage, CustomPageInfo.class);
        return Result.success(customPageInfo);
    }

    @ApiOperation("更新页面")
    @PutMapping("/{pageId}")
    public Result updatePage(@ApiParam(name = "pageId", required = true, value = "PageId") @PathVariable("pageId") String pageId,
                             @Valid @RequestBody CustomPageUpdate customPageUpdate) {
        CustomPage customPage = customPageService.updateCustomPage(pageId, customPageUpdate);
        CustomPageInfo customPageInfo = mapperFacade.map(customPage, CustomPageInfo.class);
        if(customPageUpdate.getIsDefault()!=null && customPageUpdate.getIsDefault()){ //如果是默认页则把原来默认页设成非默认
            customPageService.removeDefaultPages(customPage.getId());
        }
        return Result.success(customPageInfo);
    }

    @ApiOperation("更新页面装修")
    @PutMapping("/widgets/{pageId}")
    public Result updatePageWidget(@ApiParam(name = "pageId", required = true, value = "PageId") @PathVariable("pageId") String pageId,
                             @Valid @RequestBody CustomPageUpdate customPageUpdate) {
        CustomPage customPage = customPageService.getCustomPage(pageId);
        customPage.setPageWidgets(customPageUpdate.getPageWidgets());
        customPageService.updateCustomPage( customPage);
        CustomPageInfo customPageInfo = mapperFacade.map(customPage, CustomPageInfo.class);

        return Result.success(customPageInfo);
    }

    @ApiOperation("删除页面")
    @DeleteMapping("/{pageId}")
    public Result deletePage(@ApiParam(name = "pageId", required = true, value = "PageId") @PathVariable("pageId") String pageId) {
        customPageService.deletePage(pageId);
        return Result.success();
    }

    @ApiOperation("关键字搜索页面列表")
    @GetMapping("/search")
    public Result searchCustomPages(
            @ApiParam(name = "keyword") @RequestParam(name = "keyword", required = false) String keyword,
            @ApiParam(name = "activeOnly") @RequestParam(name = "activeOnly", defaultValue = FALSE) boolean activeOnly,
            @ApiParam(name = "page") @RequestParam(name = "page", defaultValue = "0") int page,
            @ApiParam(name = "size") @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        Page<CustomPage> pagedResult = customPageService.searchCustomPages(keyword, activeOnly, page, pageSize);
        return Result.success(pagedResult.map(customPage -> mapperFacade.map(customPage, CustomPageInfo.class)));
    }

    @ApiOperation("克隆页面")
    @PostMapping("/clone/{pageId}")
    public Result clonePage(@ApiParam(name = "pageId", required = true, value = "PageId") @PathVariable("pageId") String pageId) {
        CustomPage sourcePage = customPageService.getCustomPage(pageId);
        if(sourcePage == null){
            return Result.failed();
        }
        CustomPageCreate customPageCreate = new CustomPageCreate();
        customPageCreate.setIsDefault(false);
        customPageCreate.setActiveTime(sourcePage.getActiveTime());
        customPageCreate.setExpireTime(sourcePage.getExpireTime());
        customPageCreate.setName(sourcePage.getName());
        customPageCreate.setDescription(sourcePage.getDescription()+"-克隆");
        customPageCreate.setStatus(sourcePage.getStatus());
        CustomPage created = customPageService.createCustomPage(customPageCreate);

        CustomPageUpdate toUpdate = new CustomPageUpdate();
        toUpdate.setActiveTime(created.getActiveTime());
        toUpdate.setDescription(created.getDescription());
        toUpdate.setExpireTime(created.getExpireTime());
        toUpdate.setIsDefault(false);
        toUpdate.setName(created.getName());
        toUpdate.setStatus(created.getStatus());
        toUpdate.setPageWidgets(sourcePage.getPageWidgets());

        CustomPage customPage = customPageService.updateCustomPage(created.getId(), toUpdate);
        CustomPageInfo customPageInfo = mapperFacade.map(customPage, CustomPageInfo.class);
        return Result.success(customPageInfo);
    }

}
